package com.baizhi.cmfz.service.impl;

import com.baizhi.cmfz.service.UserService;
import com.baizhi.cmfz.service.UserThreadService;
import com.baizhi.cmfz.util.ThreadPollUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
@Service
public class UserThreadServiceImpl implements UserThreadService {
    @Autowired
    private UserService userService;
    @Override
    public Map showCountThread() throws InterruptedException {
        Map map=new HashMap();
        //创建计数器
        CountDownLatch countDownLatch=new CountDownLatch(3);
        ThreadPollUtil.executor.execute(new Runnable() {
            @Override
            public void run() {
                Map map1=userService.showDate();
                map.put("count",map1);
                countDownLatch.countDown();
            }
        });
        ThreadPollUtil.executor.execute(new Runnable() {
            @Override
            public void run() {
                List<Map> map2=userService.showSexs();
                map.put("sex",map2);
                countDownLatch.countDown();
            }
        });
        ThreadPollUtil.executor.execute(new Runnable() {
            @Override
            public void run() {
                List<Map> map3=userService.showCity();
                map.put("city",map3);
                countDownLatch.countDown();
            }
        });
        countDownLatch.await();
        return map;
    }

    @Override
    public Map showCountFuture() throws ExecutionException, InterruptedException {
        Map map=new HashMap();
        Future<String> future1=ThreadPollUtil.executor.submit(new Callable<String>() {
            @Override
            public String call() throws Exception {
                Map map1=userService.showDate();
                map.put("count",map1);
                return "haha";
            }
        });
        Future<String> future2=ThreadPollUtil.executor.submit(new Callable<String>() {
            @Override
            public String call() throws Exception {
                List<Map> map2=userService.showSexs();
                map.put("sex",map2);
                return "haha";
            }
        });
        Future<String> future3=ThreadPollUtil.executor.submit(new Callable<String>() {
            @Override
            public String call() throws Exception {
                List<Map> map3=userService.showCity();
                map.put("city",map3);
                return "haha";
            }
        });
        if("haha".equals(future1.get()) &&"haha".equals(future2.get()) &&"haha".equals(future3.get())){
            return map;
        }
        return map;
    }

    @Override
    public Map showAllCount() {
        Map map=new HashMap();
        Map map1=userService.showDate();
        List<Map> map2=userService.showSexs();
        List<Map> map3=userService.showCity();
        map.put("count",map1);
        map.put("sex",map2);
        map.put("city",map3);
        return map;
    }
}
